// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
// 引入element UI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App';
// 引入路由
import router from './router';
// 引入状态管理
import store from './vuex/store';
// 引入icon
import './assets/icon/iconfont.css'
// 
import ECharts from 'vue-echarts'




import axios from 'axios';
axios.defaults.baseURL = "http://127.0.0.1:8090/"
Vue.prototype.$axios = axios;

Vue.component('v-chart', ECharts)
    //配置axios
axios.interceptors.request.use(
    config => {
        if (window.sessionStorage.getItem("token") != null) {
            let d = JSON.parse(window.sessionStorage.getItem("token"))
            config.headers.token = d.token
        }
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

axios.interceptors.response.use(
    response => {
        if (response.status === 200) {
            return response.data;
        } else {
            Promise.reject();
        }
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);



Vue.config.productionTip = false;

// 使用element UI
Vue.use(ElementUI);
// 过滤器
import * as custom from './utils/util'

Object.keys(custom).forEach(key => {
    Vue.filter(key, custom[key])
})

// 路由拦截器
router.beforeEach((to, from, next) => {
    const token = window.sessionStorage.getItem("token");
    const ad = ['/dashbord', '/error', '/login']
    if (!token && to.path !== '/login') {
        next('/login');
    } else {
        // //判断是否有权限进入

        let menusUrl = JSON.parse(window.sessionStorage.getItem("menusUrl"))
        if (ad.indexOf(to.path) >= 0) {
            next();
        } else if (menusUrl == null) {
            next("/login");
        } else if (menusUrl.indexOf(to.path.slice(1)) < 0) {
            next("/error");
        } else {
            next();
        }
        next();
    }
})

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    store, //使用store vuex状态管理
    components: { App },
    template: '<App/>',
    data: {
        // 空的实例放到根组件下，所有的子组件都能调用
        Bus: new Vue()
    }
})